import QR from '../../utils/qrcode.js' // es6的方式引入qrcode.js工具类
// 云调用
const db = wx.cloud.database()
Page({
  data: {
    shoudata: false, //数据显示
    //设备信息
    PWidth: '',
    PHeight: '',
    //预约信息
    Tongz: '',
    //环形进度条
    value1: '',
    text1: '',
    value2: '0',
    text2: 0,
    gradientColor1: {
      '0%': '#76e7ff',
      '50%': '#f9687d',
      '100%': '#f5940d',
    },
    gradientColor2: {
      '0%': '#76e7ff',
      '50%': '#f9687d',
      '100%': '#f5940d',
    },
    //图片数据
    admin: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/guanliyuan.png?sign=c5554adde457d942e94985ad26afc385&t=1641118991',
    space: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/Saly-21.png',
    article: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/Saly-15.png',
    camera: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/article/zhaoxiangji.png?sign=88db73d59f833bde187f6ef7aeaa7a4e&t=1641287827',
    //公共变量
    id: '',
    imagePath: '',
    showscann: '', //弹出窗口
    bgcolor: '',
    Yubox: 0,
    x: '',
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    //获取设备宽度及高度
    const that = this
    wx.getSystemInfo({
      success(res) {
        that.setData({
          Pwidth: res.screenWidth,
          PHeight: res.screenHeight,
        })
      }
    })
    this.Yushow();
  },
  //预约通知显示函数
  Yushow: function () {
    db.collection('orders').where({
      orderid: 'user-YuYue'
    }).get({
      success: res => {
        //console.log(res)
        var number1 = res.data.length
        this.data.number1 = number1;
        this.setData({
          value1: 100,
          text1: number1,
          Tongz: res.data
        })
      }
    })
    db.collection('orders').where({
      status: 0
    }).get({
      success: res => {
        var number1 = this.data.number1
        var number2 = number1 - res.data.length
        //console.log('L',this.data.number1)
        var proportion = (number2 / number1) * 100;
        this.setData({
          value2: proportion,
          text2: number2
        })
      }
    })
  },
  //审核预约通知函数
  Finish: function (res) {
    //console.log('成功获取——id',res.currentTarget.dataset.id)
    var id = res.currentTarget.dataset.id //获取对应_id
    this.data.id = id
    //云开发调用
    db.collection('orders').where({
      _id: id
    }).get({
      success: res => {
        var status = res.data[0].status
        var openid = res.data[0]._openid
        var Infor1 = res.data[0].Infor1
        var Infor2 = res.data[0].Infor2
        var Infor3 = res.data[0].Infor3
        let num = res.data[0]._id + ' ' + res.data[0]._openid + Infor1 + ' ' + Infor2 //公共变量
        //console.log(status,openid,Infor1,Infor2)
        if (status == 0) {
          db.collection("orders").doc(id).update({
            //该条记录为云数据库手动添加的记录
            data: {
              status: 1
            }
          }).then(res => {
            //console.log(res)
            const pages = getCurrentPages() //更新页面数据
            const perpage = pages[pages.length - 1]
            perpage.onLoad()
          })
          //update()修改返回成功但无效，返回显示stats:{update: 0}
          //发送订阅消息
          wx.cloud.callFunction({
            name: "send",
            data: {
              openid: openid,
              place1: Infor1,
              place2: Infor2,
              ordertime: Infor3
            },
          }).then(res => {
            console.log('发送成功', res, num)
            this.getQr(num)
          }).catch(res => {
            console.log('发送失败', res)
          })
        } else {
          wx.showModal({
            cancelColor: '#000000',
            content: '请勿重复操作',
            confirmText: '确认',
            showCancel: false,
          })
        }
      }
    })
  },
  Defeat: function (res) {
    //console.log('成功获取——id',res.currentTarget.dataset.id)
    var id = res.currentTarget.dataset.id //获取对应_id
    this.setData({
      id: id
    })
    //云开发调用
    db.collection('orders').where({
      _id: id
    }).get({
      success: res => {
        var status = res.data[0].status
        var openid = res.data[0]._openid
        var Infor1 = res.data[0].Infor1
        var Infor2 = res.data[0].Infor2
        var Infor3 = res.data[0].Infor3
        //console.log(status,openid,Infor1,Infor2)
        if (status == 0) {
          db.collection("orders").doc(id).update({
            //该条记录为云数据库手动添加的记录
            data: {
              status: 2
            }
          }).then(res => {
            //console.log(res)
            const pages = getCurrentPages() //更新页面数据
            const perpage = pages[pages.length - 1]
            perpage.onLoad()
          })
          //update()修改返回成功但无效，返回显示stats:{update: 0}
          //发送订阅消息
          wx.cloud.callFunction({
            name: "send",
            data: {
              openid: openid,
              place1: Infor1,
              place2: Infor2,
              ordertime: Infor3,
            },
          }).then(res => {
            console.log('发送成功', res)
          }).catch(res => {
            console.log('发送失败', res)
          })
        } else {
          wx.showModal({
            cancelColor: '#000000',
            content: '请勿重复操作',
            confirmText: '确认',
            showCancel: false,
          })
        }
      }
    })
  },

  //返回Mine函数
  return: function () {
    wx.reLaunch({
      url: '/pages/mine/mine'
    })
  },
  // 页面跳转函数
  Gosapce: function () {
    wx.navigateTo({
      url: '/admin/space/space',
    })
  },
  Goarticle: function () {
    wx.navigateTo({
      url: '/admin/article/article',
    })
  },
  // 允许从相机和相册扫码
  scanning: function () {
    wx.scanCode({
      success(res) {
        let that = this
        var id = res.result.slice(0, 32)
        console.log('切片id', id)
        // var openid=res.result.slice(33,61)
        db.collection('orders').where({
          _id: id
        }).get({
          success: res => {
            var status = res.data[0].status
            var userinfo = res.data
            console.log('传输值', status, userinfo)
            if (status == 1) {
              wx.showToast({
                title: '预约成功',
                icon:'success',
                duration:1500
              })
            } else {
              wx.showToast({
                title: '失败',
                icon:'error',
                duration:1500
              })
            }
          }
        })
      }
    })
  },
  //弹窗关闭
  getclose: function (e) {
    this.setData({
      shoudata: false,
      showscann: false,
    })
  },
  //生成二维码的点击事件
  getQr: function (res) {
    let QRcode = res
    console.log(res)
    this.createQrCode(QRcode, 350, 350)
  },

  createQrCode: function (content, cavW, cavH) {
    let canvasId = 'mycanvas'
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(content, canvasId, cavW, cavH);
    this.canvasToTempImage(canvasId);
  },

  //获取临时缓存图片路径，存入data中
  canvasToTempImage: function (canvasId) {
    let that = this;
    var id = that.data.id
    wx.canvasToTempFilePath({
      canvasId, // 这里canvasId即之前创建的canvas-id
      success: function (res) {
        let tempFilePath = res.tempFilePath;
        db.collection("orders").doc(id).update({
          //该条记录为云数据库手动添加的记录
          data: {
            QrPath: tempFilePath,
          }
        }).then(res => {
          console.log(tempFilePath)
          that.setData({
            imagePath: tempFilePath,
          });
        })
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})